/****************************************************************************************
 * Here's where you define the overall electronics setup for your machine.
 ****************************************************************************************/

//
// CHOOSE WHICH EXTRUDER YOU'RE USING:
//
#ifndef EC_VERSION
#  define EC_VERSION 22
#endif

#if EC_VERSION == 22
#  define EXTRUDER_CONTROLLER_VERSION_2_2
#elif EC_VERSION == 20
#  define EXTRUDER_CONTROLLER_VERSION_2_0
#elif EC_VERSION == 21
#  define EXTRUDER_CONTROLLER_VERSION_2_1
#elif EC_VERSION == 32
#  define EXTRUDER_CONTROLLER_VERSION_3_2
#endif

#define TEMPERATURE_SAMPLES 5
#define SERIAL_SPEED 38400

//the address for commands to listen to
#define RS485_ADDRESS 0
#define PACKET_TIMEOUT 100

/****************************************************************************************
 * Here's where you define the way your motors are driven.
 ****************************************************************************************/
//PWM
#define MOTOR_STYLE 0
//ENCODER
//#define MOTOR_STYLE 1 
//STEPPER
//define MOTOR_STYLE 2 

//do you want to reverse the motor?
#define DELAY_FOR_STOP 5
#define MOTOR_REVERSE_DURATION 300
#define MOTOR_FORWARD_DURATION 300

/****************************************************************************************
 * Is there a heated build platform?
 ****************************************************************************************/
 
#define HAS_HEATED_BUILD_PLATFORM 0
// Define the default temperature for the heated build platform
// #define DEFAULT_PLATFORM_TEMPERATURE 0

/****************************************************************************************
 * Define the PID behavior for the heater
 ****************************************************************************************/
/// Set to true to use PID for temperature.  
/// false uses the old on-off mechanism.
#define TEMP_PID 1

/// The interval between heater updates, in milliseconds.
#define TEMP_UPDATE_INTERVAL 200
#define TEMP_PID_INTEGRAL_DRIVE_MAX 110
#define TEMP_PID_PGAIN 5.0
#define TEMP_PID_IGAIN 0.1
#define TEMP_PID_DGAIN 100.0

/****************************************************************************************
 * Here's where you define the speed PID behavior for an encoder
 ****************************************************************************************/
//#define INVERT_QUADRATURE
#define MIN_SPEED 50              //minimum PWM speed to use
#define MAX_SPEED 255             //maximum PWM speed to use
#define SPEED_ERROR_MARGIN 10     //our error margin (to prevent constant seeking)
#define SPEED_INITIAL_PGAIN 1     //our proportional gain.
#define SPEED_INITIAL_IGAIN 100   //our integral gain.
#define SPEED_INITIAL_DGAIN 10    //our derivative gain.


/****************************************************************************************
 * Here's where you define the configuration for the stepper.
 ****************************************************************************************/
#define MOTOR_STEPS 200              //number of steps per revolution
#define MOTOR_STEP_MULTIPLIER 2      //step multiplier (full = 1, half=2, etc.)

/****************************************************************************************
 * Sanguino Pin Assignment
 ****************************************************************************************/

//these are the pins for the v2.0 Extruder Controller
#if defined(EXTRUDER_CONTROLLER_VERSION_2_0) || defined(EXTRUDER_CONTROLLER_VERSION_2_1) || defined(EXTRUDER_CONTROLLER_VERSION_2_2)

#define HAS_THERMISTOR
// #define HAS_SERVOS
#define HAS_FAN
#if HAS_HEATED_BUILD_PLATFORM == 0
#define HAS_VALVE
#endif

#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3

#define RX_ENABLE_PIN 4
#define TX_ENABLE_PIN 16

#define MOTOR_1_SPEED_PIN 5
#define MOTOR_1_DIR_PIN 7
#define MOTOR_2_SPEED_PIN 6
#define MOTOR_2_DIR_PIN 8

//#define SERVO1_PIN 9
//#define SERVO2_PIN 10

#define EXTRUDER_HEATER_PIN 11
#define EXTRUDER_THERMISTOR_PIN 3

#if HAS_HEATED_BUILD_PLATFORM
  #define PLATFORM_HEATER_PIN 15
  #define PLATFORM_THERMISTOR_PIN 6
#else
  #define VALVE_PIN 15
#endif

#define FAN_PIN 12

#define DEBUG_PIN 13

//these are the pins for the v3.2 Extruder Controller
#elif defined(EXTRUDER_CONTROLLER_VERSION_3_2)

#define HAS_THERMOCOUPLE

#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3

#define RX_ENABLE_PIN 15
#define TX_ENABLE_PIN 14

#define MOTOR_2_SPEED_PIN 5
#define MOTOR_2_DIR_PIN 8
#define MOTOR_1_SPEED_PIN 6
#define MOTOR_1_DIR_PIN 7

//#define SERVO1_PIN 20
//#define SERVO2_PIN 21

#define EXTRUDER_HEATER_PIN 9

#if HAS_HEATED_BUILD_PLATFORM
  #define PLATFORM_HEATER_PIN 10
#endif

#define DEBUG_PIN 13

#define THERMOCOUPLE_CS 4
#define THERMOCOUPLE_SO 12
#define THERMOCOUPLE_SCK 13

#endif

//quadrature encoder behavior
#ifdef INVERT_QUADRATURE
#define QUADRATURE_INCREMENT speed_error--;
#define QUADRATURE_DECREMENT speed_error++;
#else
#define QUADRATURE_INCREMENT speed_error++;
#define QUADRATURE_DECREMENT speed_error--;
#endif
